package com.han.hotelplat.service.sysuserhotel.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.base.Pager;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.dao.sysuserhotel.SysUserHotelDao;
import com.han.hotelplat.pojo.hotel.Hotel;
import com.han.hotelplat.pojo.systemuser.Systemuser;
import com.han.hotelplat.pojo.sysuserhotel.SysUserHotel;
import com.han.hotelplat.service.hotel.HotelService;
import com.han.hotelplat.service.systemuser.SystemuserService;
import com.han.hotelplat.service.sysuserhotel.SysUserHotelService;

@Service
public class SysUserHotelServiceImpl implements SysUserHotelService{
	@Autowired
	private EntityManage entityManage;
	@Autowired 
	private SysUserHotelDao sysUserHotelDao;
	@Autowired
	private HotelService hotelService;
	@Autowired
	private SystemuserService systemuserService;
	@Override
	public Pager<SysUserHotel> getBindHotelByPage(String userId, Integer areaCode,
			String condition, int pageNo) {
		StringBuffer sbf = new StringBuffer( " from  tb_sysuser_hotel as sh ,tb_hotel h "
				+ " LEFT JOIN tb_hotel_hmanager AS hhm ON hhm.hotelid=h.id"
				+ " where sh.hotelid=h.id AND sh.userid='"+userId+"'" );
		if(condition != null && !condition.isEmpty()){
			sbf.append(" and (h.hotelname like '%"+condition+"%'"
					+ " or hhm.hmtel like '%"+condition+"%'"
					+ " or hhm.hmname like '%"+condition+"%')");
		}
		
		if(areaCode != null && areaCode.intValue()>100000){//区域码至少6位
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(h.area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(h.area,signed) - "+areaCode +" <= "+x;
			sbf.append(w_area);
		}
		//sbf.append(" order by sh.binddate desc");
		
		String fromSql = sbf.toString();
		sbf.insert(0, " select DISTINCT(h.id) as hotelid, h.hotelname as hotelname,h.area as area, h.hoteltype as hoteltype, "
				+ " h.state as state, h.roomcount as roomcount, "
				+ " (SELECT COUNT(subhhm.hmuserid) "
				+ " FROM tb_hotel_hmanager AS subhhm "
				+ " WHERE h.id=subhhm.hotelid and subhhm.state = 1) AS count, sh.binddate AS binddate, sh.id AS id ");
		
		short pageSize = 20;
		Pager<SysUserHotel> data = entityManage.findBySqlPage(sbf.toString(),fromSql, pageNo, pageSize,"h.id");
		List<SysUserHotel> hli = new ArrayList<SysUserHotel>();
		List li = data.getList();
		for(int i= 0 ; i<li.size(); i++){
			Object[] ory = (Object[]) li.get(i);
			SysUserHotel h = new SysUserHotel();
			if(ory[0] != null){
				h.setHotelid(Integer.parseInt(ory[0].toString()));
			}
			if(ory[1] != null){
				h.setHotelname(ory[1].toString());
			}
			if(ory[2] != null){
				if(ory[2] != null){
					h.setAreaModel(AreaHelp.getInstance().getAreaModels(ory[2].toString()));
				}
			}
			if(ory[3] != null){
				h.setHoteltype(Integer.parseInt(ory[3].toString()));
			}
			
			if(ory[4] != null){
				h.setState(Integer.parseInt(ory[4].toString()));
			}
			if(ory[5] != null){
				h.setRoomcount(Float.parseFloat(ory[5].toString()));
			}
			if(ory[6] != null){
				h.setCount(Integer.parseInt(ory[6].toString()));
			}
			if(ory[7] != null){
				h.setBinddate((Date)ory[7]);
			}
			if(ory[8] != null){
				h.setId(Integer.parseInt(ory[8].toString()));
			}
			hli.add(h);
		}
		data.setList(hli);
		return data;
	}

	@Override
	public Pager<SysUserHotel> getAllHotelByPage(String userId, Integer areaCode,
			String condition, Integer pageNo) {
		StringBuffer sbf = new StringBuffer( " from tb_hotel h "
				+ " LEFT JOIN tb_hotel_hmanager AS hhm ON hhm.hotelid=h.id  where 1=1 " );
		if(condition != null && !condition.isEmpty()){
			sbf.append(" and (h.hotelname like '%"+condition+"%'"
					+ " or hhm.hmtel like '%"+condition+"%'"
					+ " or hhm.hmname like '%"+condition+"%')");
		}
		
		if(areaCode != null && areaCode.intValue()>100000){//区域码至少6位
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(h.area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(h.area,signed) - "+areaCode +" <= "+x;
			sbf.append(w_area);
		}
		//sbf.append(" order by sh.binddate desc");
		
		String fromSql = sbf.toString();
		sbf.insert(0, " select DISTINCT(h.id) as hotelid, h.hotelname as hotelname,h.area as area, h.hoteltype as hoteltype, "
				+ " h.state as state, h.roomcount as roomcount, "
				+ " (SELECT COUNT(subhhm.hmuserid) "
				+ " FROM tb_hotel_hmanager AS subhhm "
				+ " WHERE h.id=subhhm.hotelid and subhhm.state = 1) AS count ");
		
		short pageSize = 20;
		Pager<SysUserHotel> data = entityManage.findBySqlPage(sbf.toString(),fromSql, pageNo, pageSize, "h.id");
		List<SysUserHotel> hli = new ArrayList<SysUserHotel>();
		List li = data.getList();
		for(int i= 0 ; i<li.size(); i++){
			Object[] ory = (Object[]) li.get(i);
			SysUserHotel h = new SysUserHotel();
			if(ory[0] != null){
				h.setHotelid(Integer.parseInt(ory[0].toString()));
			}
			if(ory[1] != null){
				h.setHotelname(ory[1].toString());
			}
			if(ory[2] != null){
				if(ory[2] != null){
					h.setAreaModel(AreaHelp.getInstance().getAreaModels(ory[2].toString()));
				}
			}
			if(ory[3] != null){
				h.setHoteltype(Integer.parseInt(ory[3].toString()));
			}
			
			if(ory[4] != null){
				h.setState(Integer.parseInt(ory[4].toString()));
			}
			if(ory[5] != null){
				h.setRoomcount(Float.parseFloat(ory[5].toString()));
			}
			if(ory[6] != null){
				h.setCount(Integer.parseInt(ory[6].toString()));
			}
			String sql = "select hotelid,binddate from tb_sysuser_hotel where userid='"+userId+"' and hotelid="+h.getHotelid();
			List list = entityManage.findBySql(sql);//是否绑定
			for(int m= 0 ; m<list.size(); m++){
				Object[] ory2 = (Object[]) list.get(m);
				if(ory2[1] != null){//绑定时间
					h.setBinddate((Date)ory2[1]);
					h.setBindstate(1);
				}else{
					h.setBindstate(0);
				}
			}
			hli.add(h);
		}
		data.setList(hli);
		return data;
	}

	@Override
	public List<SysUserHotel> getBindHotelByUidHids(String userId,
			JSONArray hotelIds) {
		List<SysUserHotel> re = new ArrayList<SysUserHotel>();
		String[] propertyName = new String[2];
		Object[] value = new Object[2];
		if(hotelIds != null){
			for(Object id : hotelIds){
				propertyName[0] = "userid";
				propertyName[1] = "hotelid";
				value[0] = userId;
				value[1] = id;
				List<SysUserHotel> data = entityManage.findByProperties("SysUserHotel", propertyName, value);
				if(data.size() != 0){
					re.addAll(data);
				}
			}
		}else{
				propertyName[0] = "userid";
				value[0] = userId;
				re = entityManage.findByProperties("SysUserHotel", propertyName, value);
		}
		return re;
	}

	@Override
	public void bindingHotel(String userId, JSONArray hotelIds, Integer bindAll) {
		String[] propertyName = new String[2];
		Object[] value = new Object[2];
		if(bindAll != null && bindAll.intValue() == 1){
			hotelIds = new JSONArray();
			List<Hotel> allHotel = hotelService.getAllHotel();
			for(Hotel h : allHotel){
				hotelIds.add(h.getId());
			}
		}
		if(hotelIds != null){
			for(Object id : hotelIds){
				propertyName[0] = "userid";
				propertyName[1] = "hotelid";
				value[0] = userId;
				value[1] = id;
				List<SysUserHotel> data = entityManage.findByProperties("SysUserHotel", propertyName, value);
				if(data.size() == 0){
					SysUserHotel sh = new SysUserHotel();
					sh.setUserid(userId);
					sh.setHotelid((Integer)id);
					sh.setBinddate(new Date());
					entityManage.save(sh);
				}
			}
		}
	}

	@Override
	public void unbindHotel(JSONArray hids, Integer unBindAll, String userId) {
		if(unBindAll != null && unBindAll.intValue() == 1){
			sysUserHotelDao.deleteByProperty("userid", userId);
		}else{
			if(hids != null){
				for(Object id : hids){
					sysUserHotelDao.deleteById(id);
				}
			}
		}
		if(isBindAllHotel(userId)){//曾经标记为绑定所有酒店的用户去掉标记
			Systemuser user = systemuserService.findUserByUserid(userId);
			user.setBindAllHotel(null);
			systemuserService.update(user);
		}
	}

	@Override
	public SysUserHotel getBindHotelByUidHid(String userId, Integer hotelId) {
		String hql = " from SysUserHotel where userid='"+userId+"' and hotelid="+hotelId;
		List<SysUserHotel> li = sysUserHotelDao.findByHQL(hql);
		if(li.size()>0){
			return li.get(0);
		}
		return null;
	}

	@Override
	public boolean isBindAllHotel(String userId) {
		Systemuser user = systemuserService.findUserByUserid(userId);
		if(user.getBindAllHotel() != null && user.getBindAllHotel().intValue() == 1){
			return true;
		}
		return false;
	}

	@Override
	public List<String> getAllUserBindAllHotel() {
		List<String> all = new ArrayList<String>();
		List<Systemuser> list = entityManage.findAll("Systemuser");
		for(Systemuser user : list){
			if(user.getBindAllHotel() != null && user.getBindAllHotel().intValue() == 1){
				all.add(user.getUserid());
			}
		}
		return all;
	}

	@Override
	public void bindingHotel(String userId, Integer hotelId) {
		String[] propertyName = new String[2];
		Object[] value = new Object[2];
		propertyName[0] = "userid";
		propertyName[1] = "hotelid";
		value[0] = userId;
		value[1] = hotelId;
		List<SysUserHotel> data = entityManage.findByProperties("SysUserHotel", propertyName, value);
		if(data.size() == 0){
			SysUserHotel sh = new SysUserHotel();
			sh.setUserid(userId);
			sh.setHotelid(hotelId);
			sh.setBinddate(new Date());
			entityManage.save(sh);
		}
	}
}
